import java.util.*;
/**
 * Created with IntelliJ IDEA.
 * Description: 二叉树的非递归中序遍历
 */
public class Test1 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode B = new TreeNode(2);
        TreeNode C = new TreeNode(3);
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
//        root.left = B;
        root.right = B;
//        B.left = D;
//        B.right = C;
//        C.left = F;
//        C.right = G;
        int[] nums = inorderTraversal(root);
        System.out.println(Arrays.toString(nums));
    }

    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     * @param root TreeNode类
     * @return int整型一维数组
     */
    public static int[] inorderTraversal (TreeNode root) {
        // write code here
        if(root == null) {
            return new int[0];
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        List<Integer> list = new ArrayList<>();
        while(cur != null || !stack.isEmpty()) {
            while(cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.pop();
            list.add(cur.val);
            cur = cur.right;
        }
        int[] nums = new int[list.size()];
        for (int j = 0; j < list.size(); j++) {
            nums[j] = list.get(j);
        }
        return nums;
    }
}
